package cn.iocoder.yudao.module.expert.controller.admin.expertbasedata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.expert.controller.admin.expertbasedata.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.expertbasedata.ExpertBaseDataDO;
import cn.iocoder.yudao.module.expert.service.expertbasedata.ExpertBaseDataService;

@Tag(name = "管理后台 - 专业字典")
@RestController
@RequestMapping("/expert/basedata")
@Validated
public class ExpertBasedataController {

    @Resource
    private ExpertBaseDataService basedataService;

    @PostMapping("/create")
    @Operation(summary = "创建专业字典")
    @PreAuthorize("@ss.hasPermission('expert:basedata:create')")
    public CommonResult<Long> createBasedata(@Valid @RequestBody ExpertBaseDataSaveReqVO createReqVO) {
        return success(basedataService.createBasedata(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业字典")
    @PreAuthorize("@ss.hasPermission('expert:basedata:update')")
    public CommonResult<Boolean> updateBasedata(@Valid @RequestBody ExpertBaseDataSaveReqVO updateReqVO) {
        basedataService.updateBasedata(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业字典")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('expert:basedata:delete')")
    public CommonResult<Boolean> deleteBasedata(@RequestParam("id") Long id) {
        basedataService.deleteBasedata(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专业字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('expert:basedata:query')")
    public CommonResult<ExpertBaseDataRespVO> getBasedata(@RequestParam("id") Long id) {
        ExpertBaseDataDO basedata = basedataService.getBasedata(id);
        return success(BeanUtils.toBean(basedata, ExpertBaseDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专业字典分页")
    @PreAuthorize("@ss.hasPermission('expert:basedata:query')")
    public CommonResult<PageResult<ExpertBaseDataRespVO>> getBasedataPage(@Valid ExpertBaseDataPageReqVO pageReqVO) {
        PageResult<ExpertBaseDataDO> pageResult = basedataService.getBasedataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpertBaseDataRespVO.class));
    }
    @GetMapping("/getMajorList")
    @Operation(summary = "获得专业字典分页")
    @PreAuthorize("@ss.hasPermission('expert:basedata:query')")
    public CommonResult<List<ExpertBaseDataRespVO>> getMajorList() {
        List<ExpertBaseDataDO> pageResult = basedataService.getMajorList();
        return success(BeanUtils.toBean(pageResult, ExpertBaseDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专业字典 Excel")
    @PreAuthorize("@ss.hasPermission('expert:basedata:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasedataExcel(@Valid ExpertBaseDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpertBaseDataDO> list = basedataService.getBasedataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专业字典.xls", "数据", ExpertBaseDataRespVO.class,
                        BeanUtils.toBean(list, ExpertBaseDataRespVO.class));
    }

}